package com.nuanshui.heatedloan.middleware.async.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.regex.Pattern;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;

import org.springframework.aop.support.AopUtils;
import org.springframework.util.ClassUtils;

public class CommonUtil {

	public static Class<?> getClass(Object object) {
		boolean isCglibProxy = false;
		if (AopUtils.isCglibProxy(object)) {
			isCglibProxy = true;
		}
		if (isCglibProxy || ClassUtils.isCglibProxy(object)) {
			isCglibProxy = true;
		}
		Class<?> targetClass = object.getClass();
		if (isCglibProxy) {
			targetClass = targetClass.getSuperclass();
		}
		return targetClass;
	}

	public static String buildkey(Object object, Method method) {
		ValidationUtils.checkNotNull(object);
		return new StringBuilder(getClass(object).getName()).append(".")
				.append(buildMethod(method)).toString();
	}
	
	public static String buildkey(Object object,Field field) {
		if(field == null || object == null) return null;
		return new StringBuffer(getClass(object).getName()).append(".").append(field.getName()).toString();
	}

	public static String buildMethod(Method method) {
		StringBuilder strbuilder = new StringBuilder(method.getName());
		Class<?>[] parameterTypes = method.getParameterTypes();
		if (parameterTypes != null && parameterTypes.length > 0) {
			for (Class<?> parameterType : parameterTypes) {
				strbuilder.append("#").append(parameterType.getName());
			}
		}
		return strbuilder.toString();
	}
	
	 /** 
     * 得到方法参数的名称 
     * @param cls 
     * @param clazzName 
     * @param methodName 
     * @return 
     * @throws NotFoundException 
     */  
    public static String[] getFieldsName(Class cls, String clazzName, String methodName) throws NotFoundException{  
        ClassPool pool = ClassPool.getDefault();  
        /*ClassClassPath classPath = new ClassClassPath(cls);  
        pool.insertClassPath(classPath); */ 
          
        CtClass cc = pool.get(clazzName);  
        CtMethod cm = cc.getDeclaredMethod(methodName); 
        MethodInfo methodInfo = cm.getMethodInfo();  
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();  
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);  
        if (attr == null) {  
            // exception  
        }  
        String[] paramNames = new String[cm.getParameterTypes().length];  
        int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;  
        for (int i = 0; i < paramNames.length; i++){  
            paramNames[i] = attr.variableName(i + pos); //paramNames即参数名  
        }  
        return paramNames;  
    }

	public static boolean isNumber(String number) {
		String regex = "^[0-9]+$";
		return Pattern.matches(regex, number);
	}
}
